
indexModule.controller('usersList',function($scope,$http,$modal){

    $scope.page = {
        page: 1,//当前页数
        limit: 10,// 每页显示多少条
        maxSize: 7 // 显示多少个分页按钮
    };

    //请求用户list
    var postData = {page:$scope.page.page,size:$scope.page.limit};
    userlist(postData,$scope.page.page);
    function userlist(postData,page){
        postHttp($http,config.usersList_httpUrl,postData,function(data){
            $scope.tableData = data.data.list;
            $scope.page.page = page;
            $scope.page.total = data.data.total;// list总数量
            $scope.page.numPages = Math.ceil(data.data.total/$scope.page.limit); // 总页数， 101/20 =6, 这个分页那个自动计算的值。
        });
    };

    //页码改变时
    $scope.pageChanged = function(){
        if(typeof(localStorage) === "object"){
            localStorage.setItem("limit",$scope.page.limit);
        };
        if(parseInt($scope.page.page) > parseInt($scope.page.numPages)){
            $scope.page.page = parseInt($scope.page.numPages);
        }else if(parseInt($scope.page.page) < 1){
            $scope.page.page = 1;
        }
        var postData = {page:$scope.page.page,size:$scope.page.limit};
        userlist(postData,$scope.page.page);
    };

    //新增用户
    $scope.add_list = function(){
        var modalInstance = $modal.open({
            animation: true,
            templateUrl: '/views/users/add.html',
            controller: 'saveUsersModal',
            resolve: {
                items: function () {
                    return 0;
                }
            }
        });
    };

    //修改用户密码
    $scope.modify = function(id,index){
        var modalInstance = $modal.open({
            animation: true,
            templateUrl: '/views/users/edit.html',
            controller: 'saveUsersModal',
            resolve: {
                items: function () {
                    return {id:id,right:$scope.tableData[index].right};
                }
            }
        });
    };

    //删除user
    $scope.delete = function(id){
        if(confirm(config.confirmDelete)){
            var postData = {id:id};
            postHttp($http,config.usersDel_httpUrl,postData,function(data){
                alert(config.delHttpMsg);
                window.location.reload();
            });
        }
    };
});


indexModule.controller('saveUsersModal', function ($scope,$http,$modalInstance,items) {
    if(parseInt(items.right) === 1){
        $scope.adminChecked = true;
    }else{
        $scope.defaultChecked = true;
    };
    $scope.ok = function () {
        //提交添加
        var username = $scope.username;
        var password = $scope.password;
        var rpwd = $scope.rpwd;
        var reg = /^[0-9a-zA-Z]{6,16}$/;
        var postUrl = "";
        if(items === 0 && username === undefined){ //新增用户
            $scope.modify_tips = config.usersMiss_nameVal;
        }else if(!reg.test(password)){
            return alert(config.pwdVal_isRight);
        }else if(password === undefined){
            $scope.modify_tips = config.usersMiss_pwdVal;
        }else if(password != rpwd){
            $scope.modify_tips = config.pwdVal_isSame;
        }else{
            if(items === 0){
                var right = $scope.isAdmin == "admin" ? config.adminUser : config.defaultUser;
                postUrl = config.usersAdd_httpUrl;
                var postData = {username:username,password:password,right:right};
            }else{
                var right = $scope.isAdmin == "admin" ? config.adminUser : config.defaultUser;
                postUrl = config.usersEdit_httpUrl;
                var postData = {id:items.id,password:password,right:right};
            }
            $scope.add_tips = config.saveTipHttpMsg;
            postHttp($http,postUrl,postData,function(data){
                alert(config.saveHttpMsg);
                $modalInstance.close('ok');
                window.location.reload();
            });
        }
    };

    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    };
});